package cn.chinaflame.pagodas.web;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import cn.chinaflame.pagodas.req.PaProductPointReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.constant.Sysconst;
import cn.chinaflame.pagodas.entity.PaLightsLog;
import cn.chinaflame.pagodas.entity.PaProductPoint;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaProductPointRepository;
import cn.chinaflame.pagodas.service.PaBadLightsService;
import cn.chinaflame.pagodas.service.PaLightsLogService;
import cn.chinaflame.pagodas.service.PaProductPointService;
import cn.chinaflame.pagodas.service.WrmNIService;
import cn.chinaflame.pagodas.utils.LightVO2LightLog;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import cn.chinaflame.pagodas.vo.LightVO;

/**
 * 寻灯接口
 * @author  created by boming
 * @date    2018年1月5日 上午9:45:37
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/product/point/")
public class OpenPaProductPointAPI {

	private static final Logger log = LoggerFactory.getLogger(OpenPaProductPointAPI.class);

	@Autowired
	private WrmNIService wrmNIService;
	@Autowired
	private PaProductPointRepository paProductPointRepository;
	@Autowired
	private PaProductPointService paProductPointService;
	@Autowired
	private PaBadLightsService paBadLightsService;
	@Autowired
	private PaLightsLogService paLightsLogService;
	/**
	 * 寻灯并点灯
	 * @param light_res
	 * @return
	 */
	@PostMapping("light")
	public Object light(@RequestBody List<Map<String, String>> light_res){
		log.info("寻灯并点灯light_res:{}", light_res);
		ParamPreconditions.checkArgument(light_res != null && light_res.size() > 0, CodeEnum.FAIL.getCode(), "参数为null");
		JSONArray array = new JSONArray();
		for(Map<String, String> m : light_res){
				JSONObject obj = new JSONObject();
				obj.put("lightno",m.get("location_no"));
				obj.put("obj", m.get("be_wished_name"));
				obj.put("bless",m.get("bless"));
				array.add(obj);
		}
//		String lightvos = array.toJSONString();
		 List<LightVO> result =  paProductPointService.lightCheckAndSend(array);
		 addLightLog(result);
		 return Result.create(result);
//		return Result.create(wrmNIService.lightCheckAndSend(array.toJSONString()));
	}
	
	/**
	 * 
	 * @param areano
	 * @return
	 */
	@GetMapping("light1")
	public Object light1(String areano){
		long start = System.currentTimeMillis();
		wrmNIService.lighton();
		List<String> lightnos = paProductPointService.genLightNos(areano);
//		lightnos
		for(String m : lightnos){
			JSONArray array = new JSONArray();
				JSONObject obj = new JSONObject();
				obj.put("lightno",m);
				obj.put("obj", "炎武金服炎武金服");
				obj.put("bless","炎武金服在长泰天柱山上");
				log.info(obj.toJSONString());
				array.add(obj);
				List<LightVO> vos = wrmNIService.lightSend(array.toJSONString());
				log.info("JSONArray="+JSONArray.toJSONString(vos));
		}
		log.info("用时："+((System.currentTimeMillis()-start)/1000));//"JSONArray="+JSONArray.toJSONString(vos)+
		return Result.create(null);
	}
	/**
	 * 
	 * @param areano
	 * @return
	 */
	@GetMapping("light2")
	public Object light2(String areano){
		long start = System.currentTimeMillis();
		wrmNIService.lighton();
		List<String> lightnos = paProductPointService.genLightNos(areano);
//		lightnos
		JSONArray array1 = new JSONArray();
		JSONArray array2 = new JSONArray();
		JSONArray array3 = new JSONArray();
		JSONArray array4 = new JSONArray();
		JSONArray array5 = new JSONArray();
		JSONArray array6 = new JSONArray();
		JSONArray array7 = new JSONArray();
		JSONArray array8 = new JSONArray();
		JSONArray array9 = new JSONArray();
		JSONArray array10 = new JSONArray();
		JSONArray array11 = new JSONArray();
		JSONArray array12 = new JSONArray();
		for(String m : lightnos){
			String preffix = m.substring(0,6);
			JSONObject obj = new JSONObject();
			obj.put("lightno",m);
			obj.put("obj", "炎武金服炎武金服");
			obj.put("bless","炎武金服在长泰天柱山上");
			switch(preffix) {
				case "020201":
					array1.add(obj);
					break;
				case "020202":
					array2.add(obj);
					break;
				case "020203":
					array3.add(obj);
					break;
				case "020204":
					array4.add(obj);
					break;
				case "020205":
					array5.add(obj);
					break;
				case "020206":
					array6.add(obj);
					break;
				case "020207":
					array7.add(obj);
					break;
				case "020208":
					array8.add(obj);
					break;
				case "020209":
					array9.add(obj);
					break;
				case "020210":
					array10.add(obj);
					break;
				case "020211":
					array11.add(obj);
					break;
				case "020212":
					array12.add(obj);
					break;
					default:break;
			}
		}
		for(int i=1;i<=12;i++) {
			new Thread(new Runnable(){
				public void run(){
					//List<LightVO> i = wrmNIService.lightSend((array+i).toJSONString());
				}
			}).start();
		}
		
		log.info("用时："+((System.currentTimeMillis()-start)/1000));//"JSONArray="+JSONArray.toJSONString(vos)+
		return Result.create(null);
	}
	
	

	/**
	 * 
	 * @param areano
	 * @return
	 */
	@GetMapping("lightAll")
	public Object lightAll(){
		long start = System.currentTimeMillis();
		wrmNIService.lighton();
		List<String> lightnos = paProductPointService.genLightNoAll();
//		lightnos
		JSONArray array = new JSONArray();
		for(String m : lightnos){
				JSONObject obj = new JSONObject();
				obj.put("lightno",m);
				obj.put("obj", "炎武金服炎武金服");
				obj.put("bless","炎武金服在长泰天柱山上");
				array.add(obj);
		}
		List<LightVO> vos = wrmNIService.lightSend(array.toJSONString());
		log.info("JSONArray="+JSONArray.toJSONString(vos)+"总灯数:"+array.size()+"用时："+((System.currentTimeMillis()-start)/1000));//"JSONArray="+JSONArray.toJSONString(vos)+
		return Result.create(null);
	}
	
	/**
	 * 
	 * 熄灯
	 * @param light_res
	 * @return
	 */
	@PostMapping("clear")
	public Object clear(@RequestBody List<Map<String, String>> light_res){
		log.info("清灯light_res:{}", light_res);
		ParamPreconditions.checkArgument(light_res != null && light_res.size() > 0, CodeEnum.FAIL.getCode(), "参数为null");
		List<String> param = Lists.newArrayList();
		for(Map<String, String> m : light_res){
			param.add(m.get("location_no"));
		}
		String lights =String.join(",", param);
		log.info("lights="+lights);
		List<LightVO> list = paProductPointService.lightClear(lights);//wrmNIService.lightClear(lights);
		addLightLog(list);
		List<LightVO> badlist = Lists.newArrayList();
		for(LightVO vo:list) {
			if(vo.getResult() == 0) {
				badlist.add(vo);
			}
		}
		paBadLightsService.batchAddBadLight(badlist);
		return Result.create(list);
	}
	
	/**
	 * 
	 * @param positionId
	 * @return
	 */
	@GetMapping("query/list")
	public Object queryPoint(String positionId){
		log.info("查询墙下的灯位positionId:{}", positionId);
		List<PaProductPoint> data = paProductPointRepository.findByPositionIdAndNonStatus(positionId, Sysconst.POINT_STATUS_UNINSTALL);
		return Result.create(data);
	}
	
	@GetMapping("genLightNos")
	public String genLightNos(String areano) {
		List<String> result =  paProductPointService.genLightNos(areano);
		return String.join(",",result);
	}
	@GetMapping("test")
	public String teset() {
		String result =  paProductPointService.genLight();
		return result;
	}
	/**
	 * 记录操作日志到数据库
	 * @param vos
	 */
	private void addLightLog(List<LightVO> vos) {
		if(vos != null && vos.size() > 0) {
			List<PaLightsLog> list = LightVO2LightLog.MAPPER.LightVO2LightLogs(vos);
			int[] result = paLightsLogService.batchAddLight(list);
			log.info("result={}",result);
		}
	}

	/**
	 * 根据墙号返回行和列
	 */
	@GetMapping("findAndPositionId")
	public Object queryAndPositionId (String positionId) {
		log.info("商品位置根据墙号返回行和列接口positionId:{}", positionId);
		return paProductPointService.queryAndPositionId(positionId);
	}

	/**
	 * 商品位置对应墙位置编号
	 * @param data
	 * @return
	 */
	@PostMapping("addByProductId")
	public Object addByProductId (@RequestBody String data) {
		log.info("商品位置添加精确位置表对应墙编号接口data:{}", data);
		return paProductPointService.addByProductId(data);
	}

}
